package mlib

import "errors"

// 音乐个体
type MusicEntry struct {
	Id     string
	Name   string
	Artist string
	Source string
	Type   string
}

type MusicManager struct {
	musics []MusicEntry
}

func NewMusicManager() *MusicManager {
	return &MusicManager{make([]MusicEntry, 0)}
}

func (m *MusicManager) Len() int {
	return len(m.musics)
}

func (m *MusicManager) Get(index int) (music *MusicEntry, err error) {
	if index < 0 || index >= len(m.musics) {
		return nil, errors.New("Index out of range")
	}

	return &m.musics[index], nil

	/*
		if val, ok := &m.musics[index]; ok {
			return val, nil
		}

		return nil, err
	*/
}

func (m *MusicManager) Find(name string) *MusicEntry {
	if len(m.musics) == 0 {
		return nil
	}

	for _, m1 := range m.musics {
		if m1.Name == name {
			return &m1
		}
	}

	return nil
}

func (m *MusicManager) Add(music *MusicEntry) {
	m.musics = append(m.musics, *music)
}

func (m *MusicManager) Remove(index int) *MusicEntry {
	if index < 0 || index >= len(m.musics) {
		return nil
	}

	removeMusic := &m.musics[index]

	// 取要删除的index的两边的数据 合成一个新的slice
	m.musics = append(m.musics[:index], m.musics[index+1:])

	return removeMusic
}
